package com.ag.work2.action;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ag.bean.TbEvent;
import com.ag.bean.TbPropose;
import com.ag.bean.TbProposeRecord;
import com.ag.bean.TbUser;
import com.ag.bean.TbUserDept;
import com.ag.common.AjaxNotify;
import com.ag.common.BaseAction;
import com.ag.common.EventUtil;
import com.ag.common.IPermissionService;
import com.ag.work2.service.IReportService;

@Scope("prototype")
@Controller("LZMReportAction")
public class ReportAction extends BaseAction implements SessionAware {
	private static final long serialVersionUID = 7954744545683410729L;
	/**
	 * 调用权限服务逻辑组件
	 */
	@Resource(name = "PermissionServiceImpl")
	private IPermissionService pms;
	/**
	 * 提供Map<String, Object>类型的Session变量，用来保存Struts2注入的Session上下文
	 */
	private Map<String, Object> sessionMap;
	@Resource(name = "LZMReportServiceImpl")
	private IReportService reportService;

	private List<TbUser> users;

	public List<TbUser> getUsers() {
		return users;
	}

	private List<TbUserDept> userDepts;

	public List<TbUserDept> getUserDepts() {
		return userDepts;
	}

	private String ajaxRst;

	private String eventStatus;

	private TbEvent event;

	public TbEvent getEvent() {
		return event;
	}

	public String getEventStatus() {
		return eventStatus;
	}

	private String EId;

	private String elId;

	public void setElId(String elId) {
		this.elId = elId;
	}

	/**
	 * 启动预案ID
	 */
	private String PId;

	public void setPId(String pId) {
		PId = pId;
	}

	// private List<TbPropose> proposesList;
	//	
	// public List<TbPropose> getProposesList() {
	// return proposesList;
	// }
	private TbPropose proposes;

	private TbProposeRecord proposeRecord;

	public TbProposeRecord getProposeRecord() {
		return proposeRecord;
	}

	public void setProposeRecord(TbProposeRecord proposeRecord) {
		this.proposeRecord = proposeRecord;
	}

	public void setProposes(TbPropose proposes) {
		this.proposes = proposes;
	}

	public TbPropose getProposes() {
		return proposes;
	}

	public void setEId(String eId) {
		EId = eId;
	}

	public String getAjaxRst() {
		return ajaxRst;
	}
	
	/**
	 * 直接处理
	 * 
	 * @return
	 */

	public String directly() {
		try {
			reportService.directly(proposes, proposeRecord, pms
					.getCurrentUser(sessionMap));
			ajaxRst = AjaxNotify.SUCCESS;
			return "operate";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 同意下级
	 * 
	 * @return
	 */

	public String agreeLower() {
		try {
			reportService.agreeLower(proposes, proposeRecord, pms
					.getCurrentUser(sessionMap));
			ajaxRst = AjaxNotify.SUCCESS;
			return "operate";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 获取当前事件的状态和预案启动信息
	 * 
	 * @return
	 */

	public String selectExt4Command() {
		try {
			ajaxRst = reportService.selectExt4Command((String) sessionMap
					.get(EventUtil.PROCESSING_EVENT_ID));
			return "operate";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 查找当前事件
	 * 
	 * @return
	 */
	public String findFirstReportPropose() {
		try {
			// proposeRecord = reportService
			// .findFirstReportPropose((String) sessionMap
			// .get(EventUtil.PROCESSING_EVENT_ID));
			if (reportService.findFirstReportPropose(EId) != null) {
				users = reportService.findFirstReportPropose(EId).getUsers();
				userDepts = reportService.findFirstReportPropose(EId)
						.getUserDepts();
			}
			ajaxRst = AjaxNotify.SUCCESS;
			return "find_pr_single";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}
	/**
	 * 事件评级
	 * 
	 * @return
	 */

	public String ratingEvent() {
		try {
			reportService.ratingEvent(EId, elId);
			ajaxRst = AjaxNotify.SUCCESS;
			return "operate";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 取消呈批
	 * 
	 * @return
	 */

	public String cancelPropose() {
		try {
			reportService.cancelPropose(proposeRecord, EId);
			ajaxRst = AjaxNotify.SUCCESS;
			return "operate";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 获取当前事件的状态
	 * 
	 * @return
	 */

	public String auditPropose() {
		try {
			reportService.auditPropose(proposeRecord, pms
					.getCurrentUser(sessionMap), EId);
			ajaxRst = AjaxNotify.SUCCESS;
			return "operate";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 获取当前事件的状态
	 * 
	 * @return
	 */

	public String saveAdmin() {
		try {
			reportService.saveAdmin(proposes, (String) sessionMap
					.get(EventUtil.PROCESSING_EVENT_ID), pms
					.getCurrentUser(sessionMap));
			ajaxRst = AjaxNotify.SUCCESS;
			return "operate";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}
	/*
	 * 进入直接处理时调用
	 */
	public String saveLastAdmin() {
		try {
			reportService.saveAdminSoFar(proposes, (String) sessionMap
					.get(EventUtil.PROCESSING_EVENT_ID), pms
					.getCurrentUser(sessionMap));
			ajaxRst = AjaxNotify.SUCCESS;
			return "operate";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 判断当前事件是否已经初始化过配置
	 * 
	 * @return
	 */

	public String checkPropose() {
		try {
			ajaxRst = reportService.checkPropose(EId);
			return "operate";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 判断当前事件是否已经初始化过配置
	 * 
	 * @return
	 */

	public String checkEventAdmin() {
		try {
			// proposesList=reportService.findEventPropose(EId);
			ajaxRst = reportService.checkEventAdmin(EId);
			// ajaxRst = AjaxNotify.SUCCESS;
			return "operate";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 通过事件ID查询事件详情
	 * 
	 * @return
	 */

	public String findEventByID() {
		try {
			// proposesList=reportService.findEventPropose(EId);
			event = reportService.findEventByID(EId);
			ajaxRst = AjaxNotify.SUCCESS;
			return "find_event_detail";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 查找当前所有事件
	 * 
	 * @return
	 */

	public String findEventPropose() {
		try {
			// proposesList=reportService.findEventPropose(EId);
			
			//找出成批
			proposes = reportService.findEventPropose(EId);
//			System.out.println("成批id："+proposes.getPpId());
//			Set<TbProposeRecord> records = proposes.getTbProposeRecords();
//			for(TbProposeRecord record:records)
//			{
//				System.out.println("成批记录id："+record.getNewPpId()+"\t被审核人"+record.getTbUserByPrId().getUName()+"\t成批意见："+record.getPComments()+"\t审核人："+record.getTbUserByPrCheckId().getUName());
//			}
			ajaxRst = AjaxNotify.SUCCESS;
			return "find_event_propose";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 获取当前事件的状态
	 * 
	 * @return
	 */

	public String findEventStatus() {
		try {
			// proposesList=reportService.findEventPropose(EId);
			// proposes = reportService.findEventPropose(EId);
			eventStatus = reportService.findEventStatusByID(EId);
			ajaxRst = AjaxNotify.SUCCESS;
			return "find_event_status";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 获取当前事件的状态
	 * 
	 * @return
	 */

	public String startPlan() {
		try {
			reportService.startPlan(EId, PId, pms.getCurrentUser(sessionMap));
			ajaxRst = AjaxNotify.SUCCESS;
			return "operate";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 获取当前事件的状态
	 * 
	 * @return
	 */

	public String saveProposeRecord() {
		try {
			reportService.saveProposeRecord(proposes, proposeRecord, pms
					.getCurrentUser(sessionMap));
			ajaxRst = AjaxNotify.SUCCESS;
			return "operate";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 增加呈批，reportTable4dbld.js调用，调用时呈批表里面没有记录
	 * 
	 * @return
	 */

	public String savePropose() {
		try {
			reportService.savePropose(proposes, proposeRecord, pms
					.getCurrentUser(sessionMap));
			ajaxRst = AjaxNotify.SUCCESS;
			return "operate";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.sessionMap = session;
	}

}
